package cn.wy.basic.service.impl;

import cn.wy.basic.service.VerifyService;
import cn.wy.basic.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class VerifyServiceImpl implements VerifyService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public String getImgCode(String key) {

        //获取随机验证码
        String code = VerifyCodeUtils.generateVerifyCode(4);
        //String code = StrUtils.getComplexRandomString(4);

        //将验证码存放到redis，设置5分钟过期
        redisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);

        //把验证码的值合并到图片，使用Base64编码。并返回base64编码的字符串
        System.out.println("======="+code);
        return VerifyCodeUtils.verifyCode(115, 40, code);
    }
}